package com.kun.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.kun.entity.BookInfo;
import com.kun.entity.OutbookInfo;
import com.kun.entity.PutbookInfo;
import com.kun.entity.ResponseJson;
import com.kun.service.OutbookService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OutBook")
public class OutbookController {
    @Autowired
    private OutbookService outbookService;
    @GetMapping("/OutBook/{outbookId}")
    public ResponseJson<OutbookInfo> findById(@PathVariable("outbookId")Integer outbookId){
        OutbookInfo outbookInfo =outbookService.findById(outbookId);
        ResponseJson<OutbookInfo> result = new ResponseJson<>();
        if(outbookInfo == null){
            result.setCode(500);
            result.setMsg("未找到数据");
        }else{
            result.setCode(200);
            result.setMsg("找到数据");
            result.setData(outbookInfo);
        }
        return result;
    }
    @GetMapping("/findByPage")
    public ResponseJson<Map> findByPages(@RequestParam("page") Integer pageNum
            , @RequestParam("limit") Integer pageSize
            , OutbookInfo outbookInfo){
        Map<String, Object> data = outbookService.selectByPage(pageNum, pageSize,outbookInfo);
        ResponseJson<Map> result = new ResponseJson<>();
        if (data != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(data);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
    @PostMapping("/insert")
    public  ResponseJson<OutbookInfo> insertOutBook(OutbookInfo outbookInfo,BookInfo bookInfo){
        ResponseJson<OutbookInfo> result = new ResponseJson<>();
        int i = outbookService.insertOutBook(outbookInfo,bookInfo);
        if (i==0){
            result.setCode(500);
            result.setMsg("添加失败");
        }else{
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }

    @DeleteMapping("/batchDelete")
    public ResponseJson batchDelete(Integer[] outbookIds){
        ResponseJson<OutbookInfo> result = new ResponseJson<>();
        if (outbookService.deleteOutBook(outbookIds)>0){
            result.setCode(200);
            result.setMsg("删除成功");
        }else{
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }
    @GetMapping("/exportExcel")
    public void exportExcel(OutbookInfo outbookInfo, HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("出库记录.xls","UTF-8"));

        List<OutbookInfo> list = outbookService.exportExcel(outbookInfo);

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("出库记录", "出库数据"),
                OutbookInfo.class, list);

        ServletOutputStream out = response.getOutputStream();

        workbook.write(out);

        out.flush();

        out.close();
    }
}
